package com.zf.generator.coder;

import java.util.List;


import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.internal.core.ClassFile;

import com.zf.generator.bean.JavaTemplateArgs;
import com.zf.generator.bean.JspTemplateArgs;
import com.zf.generator.bean.ProjectBean;
import com.zf.generator.templates.*;
import com.zf.generator.utils.SysFileUtil;
import com.zf.generator.utils.Tools;

public class Coder {
	private ProjectBean projectVo;
	// java代码生成
	private List<JavaTemplateArgs> javaTemplateArgsList;
	// jsp代码生成
	private List<JspTemplateArgs> jspTemplateArgsList;
	public void generateCodes(String classFullPath) throws Exception{
		projectVo = new ProjectBean();
		javaTemplateArgsList = Tools.createJavaTemplateArgsList(classFullPath);
		jspTemplateArgsList = Tools.createJspTemplateArgsList(classFullPath);

		// 定义模板
		DaoTemplate daoTemplate = new DaoTemplate();
		ServiceTemplate serviceTemplate = new ServiceTemplate();
		ActionTemplate actionTemplate = new ActionTemplate();
		JspListTemplate jspListTemplate = new JspListTemplate();
		JspInputTemplate jspInputTemplate = new JspInputTemplate();

		// -------------------------------------java代码生成-------------------------------------
//
//		monitor.worked(1);
//		monitor.subTask("java代码生成");
		for (JavaTemplateArgs javaTemplateArgs : javaTemplateArgsList) {

			try {
				// 创建包
				SysFileUtil.createPackage(javaTemplateArgs.getPackageName());

				// 生成java代码
				String javaCode = "";
				switch (javaTemplateArgs.getType()) {
				case DAO:
					javaCode = daoTemplate.generate(javaTemplateArgs);
					break;
				case SERVICE:
					javaCode = serviceTemplate
							.generate(javaTemplateArgs);
					break;
				case ACTION:
					javaCode = actionTemplate
							.generate(javaTemplateArgs);
					break;
				}
				SysFileUtil.createJavaFile(javaTemplateArgs, javaCode);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// -------------------------------------jsp代码生成-------------------------------------
//			monitor.worked(1);
//			monitor.subTask("jsp代码生成");
			for (JspTemplateArgs jspTemplateArgs : jspTemplateArgsList) {
				// 创建文件夹
				String directoryPath = projectVo.getWebInf()
						+ "/jsp/"
						+ jspTemplateArgs.getModeName()
						+ "/"
						+ jspTemplateArgs.getClassNameInfo()
								.getEntityObjectName();
				Tools.createDirectory(directoryPath);
				// 创建文件
				String filePath = directoryPath + "/"
						+ jspTemplateArgs.getJspName();
				Tools.createFile(filePath);

				// 写文件
				String jspCode = "";
				switch (jspTemplateArgs.getType()) {
				case LIST:
					jspCode = jspListTemplate.generate(jspTemplateArgs);
					break;
				case INPUT:
					jspCode = jspInputTemplate.generate(jspTemplateArgs);
					break;
				default:
					break;
				}
				Tools.writeStringToFile(filePath, jspCode);
			}
		}
	}

}
